package com.atguigu.cloud.controller;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.cloud.entities.Pay;
import com.atguigu.cloud.entities.PayDTO;
import com.atguigu.cloud.resp.ResultData;
import com.atguigu.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ViGrayThorn
 * @since 1.0.0
 * date 2025-09-01 12:54:29
 */
@RestController
@RequestMapping(value = "/pay/")
@Tag(name = "支付微服务模块", description = "支付CRUD")
public class PayController {
	@Resource
	private PayService payService;


	@PostMapping(value = "add")
	@Operation(summary = "新增", description = "新增支付流水方法,json串做参数")
	public ResultData<String> addPay(@RequestBody PayDTO payDTO) {
		Pay pay = new Pay();
		BeanUtil.copyProperties(payDTO, pay);
		int rows = payService.add(pay);
		return ResultData.success("成功插入数据，返回值：" + rows);
	}

	@DeleteMapping(value = "delete/{id}")
	@Operation(summary = "删除", description = "删除支付流水方法")
	public ResultData<Integer> deletePay(@PathVariable("id") Integer id) {
		int rows = payService.delete(id);
		return ResultData.success(rows);
	}

	@PutMapping(value = "update")
	@Operation(summary = "修改", description = "修改支付流水方法")
	public ResultData<String> updatePay(@RequestBody PayDTO payDTO) {
		Pay pay = new Pay();
		BeanUtil.copyProperties(payDTO, pay);
		int rows = payService.update(pay);
		return ResultData.success("成功修改数据，返回值：" + rows);
	}

	@GetMapping(value = "select/{id}")
	@Operation(summary = "按照ID查流水", description = "查询支付流水方法")
	public ResultData<PayDTO> selectById(@PathVariable("id") Integer id) {
		Pay pay = payService.getById(id);
		PayDTO payDTO = new PayDTO();
		BeanUtil.copyProperties(pay, payDTO);
		return ResultData.success(payDTO);
	}

	@GetMapping(value = "selectAll")
	@Operation(summary = "查询全部流水", description = "查询支付流水方法")
	public ResultData<List<PayDTO>> selectAll() {
		List<Pay> payList = payService.getAll();
		List<PayDTO> payDTOList = payList.stream()
				.map(pay -> {
					PayDTO payDTO = new PayDTO();
					BeanUtil.copyProperties(pay, payDTO);
					return payDTO;
				})
				.toList();
		return ResultData.success(payDTOList);
	}

	@Value("${server.port}")
	private String port;

	@GetMapping(value = "get/info")
	public String getInfoByConsul(@Value("${atguigu.info}") String atguiguInfo){
		return "atguigu:" + atguiguInfo + "\t" + "port:" + port;
	}
}
